package managedbean.FRM.SupplierInvoice;

import entity.SCM.MaterialOrder;
import entity.SCM.Supplier;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.FRM.SupplierInvoiceBeanLocal;
import session.stateless.SCM.SupplierBeanLocal;

@ManagedBean(name="addSupplierInvoiceManagerBean")
@ViewScoped
public class AddSupplierInvoiceManagerBean implements Serializable {
    public AddSupplierInvoiceManagerBean() {}
    
    @EJB
    private SupplierInvoiceBeanLocal supplierInvoiceBeanLocal;
    @EJB
    private SupplierBeanLocal supplierBeanLocal;
    
    private Supplier selectedSupplier;
    private MaterialOrder selectedMaterialOrder;
    private Double invoiceAmount;
    private boolean isExisting;
    private boolean isNotExisting;
    private String supplierName;
    
    private boolean isSelectedMaterialOrder;
    private String statusMessage;
    
    public void initView(PhaseEvent event){
        try
        {
            if(selectedSupplier!=null)
            {
                this.setIsExisting(true);
                this.setIsNotExisting(false);
            }
            else
            {
                this.setIsExisting(false);
                this.setIsNotExisting(true);
            }
            
            
            
        }
        catch (Exception ex)
        {
            ex.printStackTrace();            
        }
    }

    public Double getInvoiceAmount() {
        return invoiceAmount;
    }

    public void setInvoiceAmount(Double invoiceAmount) {
        this.invoiceAmount = invoiceAmount;
    }


    public boolean isIsSelectedMaterialOrder() {
        return isSelectedMaterialOrder;
    }

    public void setIsSelectedMaterialOrder(boolean isSelectedMaterialOrder) {
        this.isSelectedMaterialOrder = isSelectedMaterialOrder;
    }
    
    
    public boolean isIsNotExisting() {
        return isNotExisting;
    }

    public void setIsNotExisting(boolean isNotExisting) {
        this.isNotExisting = isNotExisting;
    }
    
    public boolean isIsExisting() {
        return isExisting;
    }

    public void setIsExisting(boolean isExisting) {        
            this.isExisting= isExisting;                
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public MaterialOrder getSelectedMaterialOrder() {
        return selectedMaterialOrder;
    }

    public void setSelectedMaterialOrder(MaterialOrder selectedMaterialOrder) {
        this.selectedMaterialOrder = selectedMaterialOrder;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Supplier getSelectedSupplier() {
        return selectedSupplier;
    }

    public void setSelectedSupplier(Supplier selectedSupplier) {
        this.selectedSupplier = selectedSupplier;
    }

    
    public void retrieve()
    {
        if (supplierName!=null)
        {
            this.setSelectedSupplier(supplierBeanLocal.findSupplierByName(supplierName));
        }
    }
    
    public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  
        
        List<Supplier> list = supplierBeanLocal.getAllSupplier();
        for (int i = 0; i < list.size(); ++i)
        {
            if (list.get(i).getSupplierName().toLowerCase().contains(query.toLowerCase()))
                results.add(list.get(i).getSupplierName());
        }        
        return results;  
    }

    
    public void createInvoice(ActionEvent event) throws IOException
    {
        System.out.println("createInvoice  "+this.getInvoiceAmount()+"   "+this.getSelectedMaterialOrder().getTotalPrice());
        if (this.getInvoiceAmount()> this.getSelectedMaterialOrder().getTotalPrice())
        {
            statusMessage="Failed. Please check again the amount entered. It cant exceed the amount of "+this.getSelectedMaterialOrder().getTotalPrice()+"";
        }
        else
        {
            Long supplierInvoiceId = supplierInvoiceBeanLocal.createSupplierInvoice(this.getSelectedMaterialOrder().getMaterialOrderId(), invoiceAmount);
            statusMessage="New invoice of "+supplierInvoiceId+" has been created successfully.";
        }
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));   
//        this.setSelectedCustomer(customerBeanLocal.findCustomerById(this.getSelectedCustomer().getRegistrationNumber()));
    }
    
    public void viewOrderDetails(ActionEvent event) throws IOException
    {
        if (selectedMaterialOrder!=null)
        {
            if (selectedMaterialOrder.getMaterialOrderState()==MaterialOrder.State.Received)
                this.setIsSelectedMaterialOrder(true);                
            else
            {
                 statusMessage="Invalid because the material order is incomplete.";
                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));   
          }
        }
        
    }
}
